// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/type/conversions.h>
#include <pollux/common/strings/string_impl.h>

namespace kumo::pollux::util {
    /// melon's tryTo doesn't ignore control characters or other unicode whitespace.
    /// We trim the string for control and unicode whitespace
    /// from both directions and return a StringView of the result.
    std::string_view trimWhiteSpace(const char *data, size_t length) {
        if (length == 0) {
            return std::string_view(data, 0);
        }

        int startIndex = 0;
        int endIndex = length - 1;
        const auto end = data + length;
        int size = 0;

        // We need to trim unicode chars and control chars
        // from left side of the string.
        for (auto i = 0; i < length;) {
            size = 0;
            auto isWhiteSpaceOrControlChar = false;

            if (data[i] & 0x80) {
                // Unicode - only check for whitespace.
                auto codePoint = utf8proc_codepoint(data + i, end, size);
                isWhiteSpaceOrControlChar =
                        pollux::functions::stringImpl::isUnicodeWhiteSpace(codePoint);
            } else {
                // Ascii - Check for both whitespace and control chars
                isWhiteSpaceOrControlChar =
                        pollux::functions::stringImpl::isAsciiWhiteSpace(data[i]) ||
                        (data[i] > 0 && data[i] < 32);
            }

            if (!isWhiteSpaceOrControlChar) {
                startIndex = i;
                break;
            }

            i += size > 0 ? size : 1;
        }

        // Trim whitespace from right side.
        for (int i = length - 1; i >= startIndex; i--) {
            size = 0;
            auto isWhiteSpaceOrControlChar = false;

            if (data[i] & 0x80) {
                // Unicode - only check for whitespace.
                utf8proc_int32_t codePoint;
                // Find the right codepoint
                while ((codePoint = utf8proc_codepoint(data + i, end, size)) < 0 &&
                       i > startIndex) {
                    i--;
                }
                isWhiteSpaceOrControlChar =
                        pollux::functions::stringImpl::isUnicodeWhiteSpace(codePoint);
            } else {
                // Ascii - check if control char or whitespace
                isWhiteSpaceOrControlChar =
                        pollux::functions::stringImpl::isAsciiWhiteSpace(data[i]) ||
                        (data[i] > 0 && data[i] < 32);
            }

            if (!isWhiteSpaceOrControlChar) {
                endIndex = i;
                break;
            }
        }

        // If we end on a unicode char make sure we add that to the end.
        auto charSize = size > 0 ? size : 1;
        return std::string_view(data + startIndex, endIndex - startIndex + charSize);
    }
} // namespace kumo::pollux::util
